﻿using System;
using System.Reflection;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.VersionControl.Common;
using Microsoft.TeamFoundation.VersionControl.Controls;

namespace TfsComLib.ItemBrowser
{
    public sealed class ChooseItemDialog : IDisposable
    {
        private readonly Form _chooseItemDialog;
        private readonly PropertyInfo _selectItemProperty;

        public ChooseItemDialog(TfsTeamProjectCollection tfs)
            : this(tfs, VersionControlPath.RootFolder)
        {
        }

        public ChooseItemDialog(TfsTeamProjectCollection tfs, string versionControlItem)
        {
            var versionControlServer = (VersionControlServer) tfs.GetService(typeof (VersionControlServer));
            var controlsAssembly = Assembly.GetAssembly(typeof (ControlAddItemsExclude));
            var vcChooseItemDialogType =
                controlsAssembly.GetType("Microsoft.TeamFoundation.VersionControl.Controls.DialogChooseItem");

            if (versionControlItem == null) versionControlItem = string.Empty;
            if (versionControlItem == VersionControlPath.RootFolder || versionControlItem.Equals(string.Empty))
            {
                var ci = vcChooseItemDialogType.GetConstructor(
                    BindingFlags.Instance | BindingFlags.NonPublic,
                    null,
                    new[] {typeof (VersionControlServer)},
                    null);
                _chooseItemDialog = (Form) ci.Invoke(new object[] {versionControlServer});
            }
            else
            {
                var initialFolder = VersionControlPath.GetFolderName(versionControlItem);
                var selectedItem = VersionControlPath.GetFileName(versionControlItem);
                var ci = vcChooseItemDialogType.GetConstructor(
                    BindingFlags.Instance | BindingFlags.NonPublic,
                    null,
                    new[] {typeof (VersionControlServer), typeof (string), typeof (string)},
                    null);
                _chooseItemDialog =
                    (Form) ci.Invoke(new object[] {versionControlServer, initialFolder, versionControlItem});
            }
            _selectItemProperty = vcChooseItemDialogType.GetProperty("SelectedItem",
                BindingFlags.Instance | BindingFlags.NonPublic);
        }

        public DialogResult DialogResult { get; set; }

        #region IDisposable Members

        public void Dispose()
        {
            if (_chooseItemDialog != null)
                _chooseItemDialog.Dispose();
        }

        #endregion

        public void ShowDialog()
        {
            _chooseItemDialog.ShowDialog();
            DialogResult = _chooseItemDialog.DialogResult;
        }

        public string GetSelectedItem()
        {
            var selectedItem = (Item) _selectItemProperty.GetValue(_chooseItemDialog, null);
            return selectedItem.ServerItem;
        }
    }
}